package com.ettractions.kiosk.mediators
{
	import com.ettractions.kiosk.components.BrochureAboutView;
	import com.ettractions.kiosk.events.BrochureSelectedEvent;
	import com.ettractions.kiosk.events.PreRenderBrochureEvent;
	import com.ettractions.kiosk.vo.BrochureVO;
	
	import flash.events.MouseEvent;
	
	import flashx.textLayout.conversion.TextConverter;
	
	import mx.events.EffectEvent;
	import mx.events.FlexEvent;
	import mx.events.PropertyChangeEvent;
	import mx.events.ResizeEvent;
	import mx.events.ScrollEvent;
	import mx.events.StateChangeEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class BrochureAboutMediator extends Mediator
	{
		[Inject]
		public var view:BrochureAboutView;
		
		override public function onRegister():void
		{
			eventDispatcher.addEventListener(PreRenderBrochureEvent.PRE_RENDER_BROCHURE, handlePreRenderBrochure);			
			eventDispatcher.addEventListener(BrochureSelectedEvent.BROCHURE_SELECTED, handleBrochureEvent);
			view.moreBtn.addEventListener(MouseEvent.CLICK, handleMoreBtnPress);
			view.moreUpBtn.addEventListener(MouseEvent.CLICK, handleMoreUpBtnPress);
			view.copy.addEventListener(ResizeEvent.RESIZE, handleChangeTextArea);
			view.scrollerCopy.viewport.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, handleScroll);
		}
		
		private function handleScroll(event:PropertyChangeEvent):void
		{
			if (event.newValue == 0)
			{
				layoutChange("top");
			}
			else if (event.newValue + view.windowCopy.height == view.copy.height)
			{
				layoutChange("bottom");	
			}
			else if (event.newValue > 0 && event.newValue + view.windowCopy.height < view.copy.height)
			{
				layoutChange("middle");
			}
			
			
		}
		
		private function updateView(brochure:BrochureVO):void
		{
			var copy:String = "<b>Ettraction Features:</b><br />"
			                + brochure.summary + "<br /><br />"
			                + "<b>Cost:</b> " + brochure.cost;
			
			
			view.copy.textFlow = TextConverter.importToFlow(copy, TextConverter.TEXT_FIELD_HTML_FORMAT);

		}
		
		private function layoutChange(layoutType:String):void
		{
			switch(layoutType)
			{
				case "none":
					view.moreBtn.visible = false;
					view.moreUpBtn.visible = false;
					view.scrollerCopy.top = 15;
					view.windowCopy.height = 315;
					break;
				case "top":
					view.moreBtn.visible = true;
					view.moreUpBtn.visible = false;
					view.scrollerCopy.top = 15;
					view.windowCopy.height = 315;
					break;
				case "middle":
					view.moreBtn.visible = true;
					view.moreUpBtn.visible = true;
					view.scrollerCopy.top = 53;
					view.windowCopy.height = 279;
					break;
				case "bottom":
					view.moreBtn.visible = false;
					view.moreUpBtn.visible = true;
					view.scrollerCopy.top = 53;
					view.windowCopy.height = 279;
					break;
			}
		}
				
		private function handleChangeTextArea(event:ResizeEvent):void
		{
			var h:Number = view.copy.height;
			if (h > view.windowCopy.height)
			{
				layoutChange("top");
			}
			else
			{
				layoutChange("none");
			}
		}
		private function handleMoreBtnPress(event:MouseEvent):void
		{
			view.scrollerCopy.verticalScrollBar.changeValueByPage(true);
			var v:Number = view.scrollerCopy.viewport.verticalScrollPosition;
		}

		private function handleMoreUpBtnPress(event:MouseEvent):void
		{
			view.scrollerCopy.verticalScrollBar.changeValueByPage(false);
			var v:Number = view.scrollerCopy.viewport.verticalScrollPosition;
		}

		private function handlePreRenderBrochure(event:PreRenderBrochureEvent):void
		{
			updateView(event.brochure);
		}
		
		private function handleBrochureEvent(event:BrochureSelectedEvent):void
		{
			if (event.brochure != null)
			{
				updateView(event.brochure);
			}
		}		
	}
}